/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_TRANSFORMATION_GPU_H
#define IMAGE_KNIFE_C_KEKE_TRANSFORMATION_GPU_H

#include "transformation_base.h"
#include "gpu_transform.h"
namespace ImageKnifePro {

class TransformationGPU : public TransformationBase {
protected:
    void SetGpuFilter(std::shared_ptr<GPUTransform> filter)
    {
        filter_ = filter;
    }
private:
    std::shared_ptr<GPUTransform> filter_ = nullptr;

    bool BeforeProcess(std::shared_ptr<ImageKnifeTask> task) final
    {
        IMAGE_KNIFE_CHECK_RETURN(filter_ == nullptr, false);
        if (filter_->InitGL()) {
            return true;
        }
        task->EchoError("GL Init Failed");
        return false;
    }

    void AfterProcess() final
    {
        filter_->DeleteGL();
    }

    std::string Process(OH_PixelmapNative* pixelmapIn, OH_PixelmapNative **pixelmapOut) final
    {
        PixemapInfo info = ParsePixelmapInfoReadPixels(pixelmapIn);
        if (info.innerBuffer == nullptr) {
            return "Read Pixels Failed";
        }

        filter_->Rendering(info.innerBuffer, info.width, info.height);

        // 创建新的pixelmap
        CreatePixelmapWithBuffer(info.innerBuffer, info, pixelmapOut);
        free(info.innerBuffer);
        return std::string();
    }
};
}
#endif //IMAGE_KNIFE_C_KEKE_TRANSFORMATION_GPU_H
